﻿using StoryMaker.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace StoryMaker.Application
{
    /// <summary>
    /// Interaction logic for ShapeControl.xaml
    /// </summary>
    public partial class ShapeControl : UserControl
    {
        public ShapeControl()
        {
            InitializeComponent();
            SetBrushes();
        }

        protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
        {
            base.OnPropertyChanged(e);
            if (e.Property == StoryShapeProperty)
            {
                SetStoryShapeStyle();
            }
            if (e.Property == FillShapeProperty)
            {
                SetBrushes();
            }
        }

        private void SetBrushes()
        {
            DependencyProperty activeBrushProperty = FillShape ? FillBrushProperty : StrokeBrushProperty;
            DependencyProperty inactiveBrushProperty = !FillShape ? FillBrushProperty : StrokeBrushProperty;
            Binding binding = new Binding("Brush");
            binding.Mode = BindingMode.OneWay;
            binding.Source = this;
            binding.NotifyOnSourceUpdated = true;
            binding.NotifyOnTargetUpdated = true;
            SetBinding(activeBrushProperty, binding);
            SetValue(inactiveBrushProperty, EmptyBrush);
        }

        public static readonly Brush EmptyBrush = Brushes.Transparent;

        private static IEnumerable<DependencyProperty> LocalDependencyProperties
        {
            get
            {
                yield return BrushProperty;
                yield return FillShapeProperty;
                yield return HeightProperty;
                yield return WidthProperty;
            }
        }

        public Brush Brush
        {
            get { return (Brush)GetValue(BrushProperty); }
            set { SetValue(BrushProperty, value); }
        }
        public static readonly DependencyProperty BrushProperty = DependencyProperty.Register("Brush", typeof(Brush), typeof(ShapeControl), new UIPropertyMetadata(Brushes.Red));

        public Brush FillBrush
        {
            get { return (Brush)GetValue(FillBrushProperty); }
            set { SetValue(FillBrushProperty, value); }
        }
        public static readonly DependencyProperty FillBrushProperty = DependencyProperty.Register("FillBrush", typeof(Brush), typeof(ShapeControl), new UIPropertyMetadata(Brushes.Red));

        public Brush StrokeBrush
        {
            get { return (Brush)GetValue(StrokeBrushProperty); }
            set { SetValue(StrokeBrushProperty, value); }
        }
        public static readonly DependencyProperty StrokeBrushProperty = DependencyProperty.Register("StrokeBrush", typeof(Brush), typeof(ShapeControl), new UIPropertyMetadata(Brushes.Red));

        public bool FillShape
        {
            get { return (bool)GetValue(FillShapeProperty); }
            set { SetValue(FillShapeProperty, value); }
        }
        public static readonly DependencyProperty FillShapeProperty = DependencyProperty.Register("FillShape", typeof(bool), typeof(ShapeControl), new UIPropertyMetadata(true));

        public StoryShape StoryShape
        {
            get { return (StoryShape)GetValue(StoryShapeProperty); }
            set { SetValue(StoryShapeProperty, value); }
        }
        public static readonly DependencyProperty StoryShapeProperty = DependencyProperty.Register("StoryShape", typeof(StoryShape), typeof(ShapeControl), new UIPropertyMetadata(default(StoryShape)));

        public void SetStoryShapeStyle()
        {
            Style style = new Style(typeof(ShapeControl), (Style)Resources[typeof(ShapeControl)]);
            foreach (DependencyProperty property in LocalDependencyProperties)
            {
                Binding binding = new Binding();
                binding.Source = StoryShape;
                binding.Path = new PropertyPath(property.Name);
                style.Setters.Add(new Setter(property, binding));
            }
            this.Style = style;
        }
    }
}
